package com.zto.sch.base.thread;

/**
 * @program: sch-diy-parent
 * @description:
 * @author: sch
 * @create: 2021-01-09 23:07
 **/
public class ThreadDemo_lock {
    /**
     * 我现在希望 A 在打印完 1 后，再让 B 打印 1, 2, 3，最后再回到 A 继续打印 2, 3
     *
     * @param args
     */
//如果线程B先执行，线程A会一直等待
    public static void main(String[] args) {
        Object lock = new Object();
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                //System.out.println("thread-A：" + 1); 写在这里B线程会先执行
                synchronized (lock) {
                    System.out.println("thread-A：" + 1);
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("thread-A：" + 2);
                    System.out.println("thread-A：" + 3);
                }

            }
        });
        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock) {
                    System.out.println("thread-B：" + 1);
                    System.out.println("thread-B：" + 2);
                    System.out.println("thread-B：" + 3);
                    lock.notify();
                }
            }
        });
        threadA.start();
        threadB.start();
    }
}
